using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Boss : MonoBehaviour
{
    public Image bossbar;
    public GameObject player;
    public GameObject bullet1, bullet2, bullet3;
    public GameObject HurtNumber;
    public GameObject energyDrop;
    public GameObject itemDrop;
    public GameObject showanim;
    private Renderer render;
    private Animator anim;
    private Collider2D coll;
    private Rigidbody2D rb;

    public float HP, MaxHP;
    public float RushSpeed;
    private int stage;
    private int BulletWave = 1;
    private float timer,timer1;
    private Vector3 directionPlayer;
    private int HurtNum;
    // Start is called before the first frame update
    void Start()
    {
        render = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
        coll = GetComponent<Collider2D>();
        rb = GetComponent<Rigidbody2D>();
        HP = MaxHP;
        stage = 1;
        player = GameObject.FindWithTag("Player");
        timer = 1.5f;
        timer1 = 1.5f;
        Time.timeScale = .1f;
        showanim.SetActive(true);
        Invoke("ReTime", .25f);
    }

    // Update is called once per frame
    void Update()
    {
        directionPlayer = (player.transform.position - transform.position);
        if (timer > 0)
        {
            timer -= Time.deltaTime;
        }
        else
        {
            timer = 0;
        }
        if (timer1 > 0)
        {
            timer1 -= Time.deltaTime;
        }
        else
        {
            timer1 = 0;
        }
        if (stage==1)
        {
            Stage1();
        }
        else if (stage==2)
        {
            Stage2();
        }
        if (HP < 0&&stage==2)
        {
            //GameObject.FindWithTag("Player").GetComponent<PlayerCon>().parameterPlayer.killNum++;
            stage = 3;
            player.GetComponent<PlayerCon>().parameterPlayer.killNum++;
            coll.enabled = false;
            anim.Play("Death");
            HP = 0;
            coll.GetComponent<SpriteRenderer>().sortingOrder--;
            rb.velocity = new Vector3(0, 0, 0);
            Instantiate(itemDrop,transform.position,transform.rotation);
        }
        else if (stage == 1&&HP < MaxHP / 2)
        {
            stage = 2;
            timer = 0;
            timer1 = 0;
            for (int i = 1; i <= 10; i++)
            {
                float a = Random.Range(-100, 100);
                float b = Random.Range(-50, 50);
                GameObject instance = GameObject.Instantiate(energyDrop, new Vector3(
                    transform.position.x + a / 100, transform.position.y + b / 100, transform.position.z), transform.rotation);
            }
        }
        bossbar.transform.localScale = new Vector3((float)HP / MaxHP, 1, 1);
    }
    void Stage1()
    {
        if (BulletWave > 100)
        {
            BulletWave = 1;
        }
        if (timer == 0)
        {
            if (BulletWave == 1)
            {
                Danmu(12, 0, bullet1);
                BulletWave++;
                timer = .5f;
            }
            else if (BulletWave == 2)
            {
                Danmu(12, 6, bullet2);
                BulletWave++;
                timer = .5f;
            }
            else if (BulletWave == 3)
            {
                Danmu(12, 0, bullet3);
                BulletWave++;
                timer = 2.5f;
            }
            else if (BulletWave ==4|| BulletWave == 6||BulletWave == 8)
            {
                Danmu(15, 0, bullet1);
                BulletWave++;
                timer = .8f;
            }
            else if(BulletWave == 5 || BulletWave == 7 || BulletWave == 9)
            {
                Danmu(15, 12, bullet1);
                BulletWave++;
                timer = .8f;
            }
            else if (BulletWave == 10)
            {
                Danmu(15, 0, bullet3);
                BulletWave++;
                timer = 2.5f;

            }
            else if (BulletWave >=11&& BulletWave <= 18)
            {
                for (int i = 0; i <= 45; i=i+5)
                {
                    for (int j = 1; j <= 4; j++)
                    {
                        BossShoot(transform.position, Quaternion.AngleAxis(-22.5f+i+90*j, Vector3.forward) * new Vector3(0, 1, 0),
                            bullet2);
                    }
                }
                timer = (float)(19 - BulletWave) / 10;
                BulletWave++;
                timer1 = 1f;
            }
            else if (BulletWave >= 19 && BulletWave <100)
            {
                for (int i = 0; i <= 45; i = i + 9)
                {
                    for (int j = 1; j <= 4; j++)
                    {
                        BossShoot(transform.position, Quaternion.AngleAxis(-22.5f + i + 90 * j+(BulletWave-20)*2, Vector3.forward) * new Vector3(0, 1, 0),
                            bullet2);
                    }
                }
                timer = .05f;
                BulletWave++;
                timer1 = 1f;
            }
            else if (BulletWave == 100)
            {
                BulletWave++;
                timer = 7f;
                timer1 = 4.5f;
            }
        }
        if (timer1 == 0)
        {
            GameObject bullet = ObjectPool.Instance.GetObject(bullet2);
            bullet.transform.position = transform.position;
            bullet.GetComponent<bullet>().SetSpeed(directionPlayer);
            timer1 = (float)Random.Range(20, 100) / 100;
        }
    }
    void Danmu(int num, float angel, GameObject bulletPrefab)
    {
        for (int i = 1; i <= num; i++)
        {
            BossShoot(transform.position, Quaternion.AngleAxis(360 / num * i + angel, Vector3.forward) * new Vector3(0, 1, 0),
                bulletPrefab);
        }
    }
    void BossShoot(Vector3 position, Vector3 direction,GameObject bulletPrefab)
    {
        GameObject bullet = ObjectPool.Instance.GetObject(bulletPrefab);
        bullet.transform.position = position;
        bullet.transform.right = direction;
        bullet.GetComponent<bullet>().SetSpeed(direction);
    }
    void Stage2()
    {
        if (timer == 0)
        {
            if (player.transform.position.x > transform.position.x) { transform.localScale = new Vector3(1, 1, 1); }
            else { transform.localScale = new Vector3(-1, 1, 1); }
            anim.Play("Rush");
            rb.velocity = directionPlayer.normalized * RushSpeed;
            timer = 5f;
        }
        else if (timer >1 && timer <2)
        {
            anim.Play("Idle");
            rb.velocity = new Vector3(0, 0, 0);
            timer = 1;
        }
        if (timer > 1&&timer1==0)
        {
            BossShoot(transform.position, Quaternion.AngleAxis(90, Vector3.forward) * rb.velocity.normalized, bullet2);
            BossShoot(transform.position, Quaternion.AngleAxis(-90, Vector3.forward) * rb.velocity.normalized, bullet2);
            timer1 = .3f;
        }
        if (timer > 1)
        {
            if (transform.position.x < -91.7 && transform.localScale.x == -1)
            {
                transform.localScale = new Vector3(1, 1, 1);
            }
            if (transform.position.x > -73.3 && transform.localScale.x == 1)
            {
                transform.localScale = new Vector3(-1, 1, 1);
            }
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Bullet_Player") || collision.gameObject.layer == LayerMask.NameToLayer("Knife"))
        {
            if (collision.GetComponent<bullet>() != null)
            { HurtNum = collision.GetComponent<bullet>().damage; }
            if (collision.GetComponent<bullet2>() != null)
            { HurtNum = collision.GetComponent<bullet2>().damage; }
            Vector3 vector = new Vector2((float)UnityEngine.Random.Range(-20, 20) / 100, (float)UnityEngine.Random.Range(100, 140) / 100);
            GameObject gb = Instantiate(HurtNumber, transform.position + vector, transform.rotation);
            gb.GetComponent<TextMesh>().text = HurtNum.ToString();
            if (stage == 1) { HP -= HurtNum * 0.7f; }
            else { HP -= HurtNum; }
            render.material.color = Color.red;
            Invoke("ReColor", 0.05f);
        }
    }
    void ReColor()
    {
        render.material.color = Color.white;
    }
    void ReTime()
    {
        Time.timeScale = 1f;
        showanim.SetActive(false);
    }
}
